#include "screen.h"
#include "ssd1315/ssd1315.h"
#include "srt/srt.h"
#include "data_center/data_center.h"
#include "common/common.h"
#include "i2c/i2c.h"
#include "pico/stdlib.h"
#include <stdio.h>

#define LOG_TAG "SCREEN"
#include "log/log.h"


enum DisplayStatusTag
{
    WaitCollect,
    DisplayNormal,
};


/**
 * TScreen 屏幕相关 结构体
 */
struct TScreenTag
{
    Bool   is_init_;          ///< 是否初始化
    UInt32 last_check_tick_;  ///< 上次检查时间

    enum DisplayStatusTag display_status_;   ///< 显示状态
};
typedef struct TScreenTag TScreen;

static TScreen obj_ = {
    .is_init_         = False,
    .last_check_tick_ = 0,
    .display_status_  = WaitCollect,
};
static TScreen* self = &obj_;


static void DoWriteI2c(UInt8 addr, UInt8 *data, UInt16 data_len)
{
    TI2CSendData(addr, data, data_len);
}

static void DisplayUI(void)
{
    // 清屏
    TSSD1315_Clear();
    // 打印电压描述
    TSSD1315_ShowString(0, 0, "PWR:    ACC:   ", kFont8x16);

    // 打印电压描述
    TSSD1315_ShowString(0, 3, "Volt:         V", kFont8x16);

    // 打印电流描述
    TSSD1315_ShowString(0, 6, "Curr:        mA", kFont8x16);
}

Bool TScreenInit()
{
    TDataCenterInit(kDataCenterUserScreen);

    TSSD1315_Init(DoWriteI2c);

    self->is_init_ = True;
}

Bool TScreenFini()
{
    self->is_init_ = False;

    TDataCenterFini(kDataCenterUserScreen);
}

static void DealMsg(TDataCenterMsg *msg)
{
    if (msg != NULL)
    {
        LOG_D("Get Data Center msg! msg type: %d", msg->msg_type);

        switch (msg->msg_type)
        {
        case kDataCenterMsgTypeUpdateScreen:
            self->last_check_tick_ = 0;
            break;
        default:
            LOG_W("Unknown msg type, ignore msg.");
            break;
        }
    }
}

void TScreenRunOnce(UInt32 cur_ticks)
{
    if (self->is_init_)
    {
        // 消息交互
        TDataCenterMsg *msg = TDataCenterPopMsg(kDataCenterUserScreen);

        if (msg != NULL)
        {
            DealMsg(msg);
            TDataCenterDestroyMsg(msg);
        }

        switch (self->display_status_)
        {
        case WaitCollect:
        {
            if (cur_ticks - self->last_check_tick_ > 2000)  ///< 显示2秒logo，再开始显示UI的
            {
                // 显示UI
                LOG_I("Display UI");
                DisplayUI();
                self->display_status_  = DisplayNormal;
                self->last_check_tick_ = 0;
            }
        }
            break;
        case DisplayNormal:
        {
            if (cur_ticks - self->last_check_tick_ > 500)  ///< 每500ms，刷新一下数据
            {
                Float volt, curr;
                Bool  pwr_on, acc_on;
                // 更新数据
                if (TSrtGetFloat(kSrtFloatBusVolt, &volt))  ///< 获取总线电压
                {
                    TSSD1315_ShowFloat(8 * 6, 3, volt, 6, kFont8x16);
                }

                if (TSrtGetFloat(kSrtFloatBusCurr, &curr))  ///< 获取总线电流
                {
                    TSSD1315_ShowFloat(8 * 6, 6, curr, 6, kFont8x16);
                }

                if (TSrtGetBool(kSrtBoolPwrStatus, &pwr_on) && TSrtGetBool(kSrtBoolAccStatus, &acc_on))
                {
                    if (pwr_on && volt == 0)
                    {
                        // 打印提示
                        TSSD1315_ShowString(0, 0, "PWR SWITCH OFF!", kFont8x16);
                    }
                    else
                    {
                        UInt8 print_str[16] = {0};

                        snprintf(print_str, sizeof(print_str), "PWR:%s ACC:%s", pwr_on ? "ON " : "OFF", acc_on ? "ON " : "OFF");

                        TSSD1315_ShowString(0, 0, print_str, kFont8x16);
                    }
                }

                self->last_check_tick_ = cur_ticks;
            }
        }
            break;
        }
    }
}
